package date_02

// https://leetcode-cn.com/problems/employee-importance/
// 690. 员工的重要性
// 2021年5月2日 22点50分
type Employee struct {
	Id           int
	Importance   int
	Subordinates []int
}

func getImportance(employees []*Employee, id int) int {
	if len(employees) == 0 {
		return 0
	}
	var t *Employee
	for _, e := range employees {
		if e.Id == id {
			t = e
		}
	}
	if t == nil {
		return 0
	}
	var recur func(*Employee) int
	recur = func(em *Employee) int {
		sub := em.Subordinates
		r := em.Importance
		dict := make(map[int]bool)
		for _, item := range sub {
			dict[item] = true
		}
		for _, item := range employees {
			if dict[item.Id] {
				r += recur(item)
			}
		}
		return r
	}
	return recur(t)
}

func getImportance2(employees []*Employee, id int) int {
	if len(employees) == 0 {
		return 0
	}
	mp := make(map[int]*Employee)
	for _, item := range employees {
		mp[item.Id] = item
	}
	var recur func(id int) int
	recur = func(id int) int {
		e := mp[id]
		r := e.Importance
		for _, item := range e.Subordinates {
			r += recur(item)
		}
		return r
	}
	return recur(id)
}
